package com.pshcp.api;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.tool.CommonTool;
import com.pshcp.ao.common.CommonAO;
import com.pshcp.ao.wechat.CreateWxaCodeUnlimitAO;
import com.pshcp.common.config.WxMaConfiguration;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Base64;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2023/2/18 17:55
 */
@Api(tags = {"通用接口-微信"})
@Slf4j
@RestController
@RequestMapping("/api/wechat")
public class WechatAPI {

    @ApiOperation("获取不限制的小程序码")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取不限制的小程序码")
    @PostMapping("createWxaCodeUnlimit")
    public ResultDTO<String> createWxaCodeUnlimit(@RequestBody CreateWxaCodeUnlimitAO createWxaCodeUnlimitAO) throws Exception {
        CommonTool.checkString(createWxaCodeUnlimitAO.getScene(), "90000012");
        CommonTool.checkString(createWxaCodeUnlimitAO.getPage(), "90000012");
        final WxMaService wxService = WxMaConfiguration.getMaService();
        byte[] wxaCodeUnlimitBytes = wxService.getQrcodeService().createWxaCodeUnlimitBytes(createWxaCodeUnlimitAO.getScene(), createWxaCodeUnlimitAO.getPage(), createWxaCodeUnlimitAO.isCheckPath(), createWxaCodeUnlimitAO.getEnvVersion(), createWxaCodeUnlimitAO.getWidth(), createWxaCodeUnlimitAO.isAutoColor(),
                createWxaCodeUnlimitAO.getLineColor(), createWxaCodeUnlimitAO.isHyaline());
        String base64 = Base64.getEncoder().encodeToString(wxaCodeUnlimitBytes);
        return ResultBuilder.success( "data:image/png;base64,"+base64);
    }

    @ApiOperation("获取医生的二维码")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取医生的二维码")
    @PostMapping("getDoctorWxaCode")
    public ResultDTO<String> createWxaCodeUnlimit(@RequestBody CommonAO commonAO) throws Exception {
        CommonTool.checkObj(commonAO.getId(), "90000012");
        String scene="type=0&id="+commonAO.getId();
        String page="pages/index/index";
        CreateWxaCodeUnlimitAO createWxaCodeUnlimitAO=new CreateWxaCodeUnlimitAO();
        final WxMaService wxService = WxMaConfiguration.getMaService();
        byte[] wxaCodeUnlimitBytes = wxService.getQrcodeService().createWxaCodeUnlimitBytes(scene, page, createWxaCodeUnlimitAO.isCheckPath(), createWxaCodeUnlimitAO.getEnvVersion(), createWxaCodeUnlimitAO.getWidth(), createWxaCodeUnlimitAO.isAutoColor(),
                createWxaCodeUnlimitAO.getLineColor(), createWxaCodeUnlimitAO.isHyaline());
        String base64 = Base64.getEncoder().encodeToString(wxaCodeUnlimitBytes);
        return ResultBuilder.success( "data:image/png;base64,"+base64);
    }
}
